package seqlist.oj;

/**
 * @Author: yuisama
 * @Date: 2019/12/11 12:11
 * @Description:LeetCode142-环形链表入口—双指针法
 * https://leetcode-cn.com/problems/linked-list-cycle-ii/
 */
public class Solution142 {
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null) {
            return null;
        }
        ListNode fast = head,low = head;
        while (true) {
            if (fast == null || fast.next == null) {
                return null;
            }
            fast = fast.next.next;
            low = low.next;
            if (low == fast) {
                break;
            }
        }
        // 此时low即为相遇点
        fast = head;
        while (low != fast) {
            low = low.next;
            fast = fast.next;
        }
        return low;
    }
}
